//
//  WHYChannel.m
//  News163
//
//  Created by 王洪运 on 15/12/30.
//  Copyright © 2015年 王洪运. All rights reserved.
//

#import "WHYChannel.h"

@implementation WHYChannel

+(NSArray<WHYChannel *> *)channelList {
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"topic_news.json" ofType:nil];
        
        NSData *data = [NSData dataWithContentsOfFile:path];
        
        NSDictionary *JSONDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:NULL];
        
        NSArray *listArr = JSONDict.allValues.firstObject;
        
        NSMutableArray *mArr = [NSMutableArray array];
        
        [listArr enumerateObjectsUsingBlock:^(NSDictionary *  _Nonnull dict, NSUInteger idx, BOOL * _Nonnull stop) {
            WHYChannel *channel = [WHYChannel channelWithDict:dict];
            [mArr addObject:channel];
        }];
        
//        将模型按tid从小到大排序
        NSArray *modelArr = [mArr sortedArrayUsingComparator:^NSComparisonResult(WHYChannel *  _Nonnull obj1, WHYChannel *  _Nonnull obj2) {
            return [obj1.tid compare:obj2.tid];
        }];
    
    return modelArr;
    
}

-(instancetype)initWithDict:(NSDictionary *)dict {
    if (self == [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

+(instancetype)channelWithDict:(NSDictionary *)dict {
    return [[self alloc] initWithDict:dict];
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key { }

-(NSString *)description {
    return [NSString stringWithFormat:@"tid: %zd, tname: %@",self.tid, self.tname];
}

@end
